package com.miniboy.imageloader_sample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

/**
 * 作者 ${郭鹏飞}.<br/>
 */
public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private String[] data;

    public ImageAdapter(Context context, String[] data) {
        mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        View view;

        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.image_item,
                    parent, false);
            holder = new ViewHolder();
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        holder.mImageView = (ImageView) view.findViewById(R.id.id_image_item);

        FLYApplication.getInstance().getImageLoader().
                into(holder.mImageView).url(data[position]).load();

        return view;
    }

    private final class ViewHolder {
        ImageView mImageView;
    }
}
